package com.fly.sealedcontainer;

import com.fly.sealedcontainer.handler.Handler;
import com.fly.sealedcontainer.util.MyUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.util.StringUtils;

import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

//@MapperScan(basePackages = "com.fly.sealedcontainer.mapper")
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class SealedContainerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SealedContainerApplication.class, args);

        ExecutorService executorService = MyUtil.theadPool();
        // 灭火器，液化气罐，乙炔瓶，千斤顶，液压油缸，避震器
//        List<String> wordList = Arrays.asList("灭火器", "液化气罐", "乙炔瓶", "千斤顶", "液压油缸", "避震器");
//        List<String> wordList = Arrays.asList("钢机");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n--> 请输入关键词，多个关键词以空格分隔（输入n结束程序）");
            String s = scanner.nextLine();
            if (StringUtils.hasText(s) && "n".equals(s.toLowerCase())) {
                System.out.println("\n--> 结束程序");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }

            if (StringUtils.hasText(s)) {
                String[] keywords = s.split(" ");
                CountDownLatch latch = new CountDownLatch(keywords.length);
                for (String word : keywords) {
                    executorService.execute(() -> {
                        Handler handler = new Handler(word);
                        // 任务执行
                        handler.execute();
                        latch.countDown();
                    });
                }
                try {
                    latch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
